from django.contrib.auth.models import User
from django.db import models

# Create your models here.
# 文章详情模型
from utils.modelsMixin import ModelSetMixin, DateTimeModelMixin


# 标签模型
class Label(DateTimeModelMixin):
    name = models.CharField(max_length=20, verbose_name="标签名", unique=True)

    def __str__(self) -> str:
        return self.name


# 文章模型
class Article(ModelSetMixin):
    STATUS_CHOICES = (
        (0, "草稿"),
        (1, "发布")
    )
    # 文章标题
    title = models.CharField(max_length=100, verbose_name="文章标题")
    # 文章摘要
    digest = models.TextField(verbose_name="文章摘要")
    # 文章内容
    content = models.TextField(verbose_name="文章内容")
    # 浏览量
    page_view = models.IntegerField(default=0, verbose_name="浏览量")
    # 优先级
    priority = models.IntegerField(default=0, verbose_name="优先级")
    # 状态
    status = models.IntegerField(default=0, verbose_name="状态", choices=STATUS_CHOICES)
    # 作者
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    label = models.ForeignKey(Label, on_delete=models.CASCADE)

    def __str__(self) -> str:
        return "标题: %s, 作者: %s" % (self.title, self.user)

    class Meta:
        # 排序
        ordering = ['-priority', 'page_view', '-create_time']


# 评论模型
class Comment(ModelSetMixin):
    STATUS_CHOICES = (
        (0, "正常"),
        (1, "禁用")
    )
    content = models.TextField(verbose_name="评论内容")
    status = models.IntegerField(default=0, verbose_name="状态", choices=STATUS_CHOICES)
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    # 父级评论 下面的评论可以有多个 外键是可以关联自己的 对自身主键进行关联 字段值可以为空
    parent_comment = models.ForeignKey("self", null=True, blank=True, verbose_name="父级评论", on_delete=models.CASCADE,
                                       related_name="parent_comment_set")
    # 回复评论 被回复的评论下面可以有多个 管理名 关联的表名小写_set comment_set
    reply_comment = models.ForeignKey("self", null=True, blank=True, verbose_name="回复评论", on_delete=models.CASCADE,
                                      related_name="reply_comment_set")

    def __str__(self):
        return "%s:%s" % (self.user.username, self.content)

    class Meta:
        # 依据创建时间进行排序
        ordering = ["-create_time"]
